package com.topboya.officeuser.controller.parse.util;

import com.alibaba.fastjson.JSONObject;
import com.topboya.officeuser.controller.parse.read.TencentRead;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @className ParseLatAndLngUtil
 * @Author xiaxin
 * @Date 2020/12/31 18:26
 * @Version 1.0
 **/
public class ParseLatAndLngUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(ParseLatAndLngUtil.class);

    public static void successAndFailExcelList(Integer headLine, Integer addressPosit, Integer lngPosit, Integer latPosit, List<List<String>> lists, List<List<String>> successList, List<List<String>> failList) throws Exception {
        TencentRead tr = new TencentRead();

        for (int i = 0;i<headLine;i++){
            successList.add(lists.get(i));
            failList.add(lists.get(i));
        }
        for (int i = 0; i < lists.size(); i++) {
            List<String> strings = lists.get(i);
            if(i>=headLine) {
                    Thread.sleep(500);
                    String location = tr.getLocation(strings.get(addressPosit - 1));
                    LOGGER.info(location);
                    JSONObject jsonObject = JSONObject.parseObject(location);
                    if(jsonObject!=null){
                    JSONObject result = jsonObject.getJSONObject("result");
                    if (result != null) {
                        JSONObject j_location = result.getJSONObject("location");
                        if (j_location != null) {
                            if (j_location.get("lng") != null && j_location.get("lat") != null) {
                                String lng = j_location.get("lng").toString();
                                String lat = j_location.get("lat").toString();

                                strings.set(lngPosit-1, lng);
                                strings.set(latPosit-1, lat);
                                successList.add(strings);
                            } else {
                                strings.add("未解析到经纬度");
                                failList.add(strings);
                            }
                        } else {
                            strings.add("未解析到经纬度");
                            failList.add(strings);
                        }

                    } else {
                        strings.add("未解析到经纬度");
                        failList.add(strings);
                    }
                }else {
                        strings.add("未解析到经纬度");
                        failList.add(strings);
                    }
                }
            }
    }

}
